// src/controller/user.ts
// GET /user/1
import { Controller, Get, Inject, Param } from '@midwayjs/decorator';
import { User } from '../../interface';
import { Context } from '@midwayjs/koa';

/**
 * Router Params
 * 如果路由上使用 :xxx 的格式来声明路由，那么参数可以通过 ctx.params 获取到。
 */
@Controller('/api/controller/param/router')
export class UserController {
  /**
   * 示例：从装饰器获取
   * @param uid
   */
  @Get('getRouterParamFromDecorator/:uid')
  async getRouterParamFromDecorator(@Param('uid') uid: string): Promise<User> {
    // xxxx
    console.log('uid;', uid);
    return {
      id: 1,
      name: 'testAtQuery',
      age: 1,
    };
  }

  /**
   * 示例：从 API 获取
   */
  @Inject()
  ctx: Context;

  @Get('getRouterParamFromApi/:uid')
  async getRouterParamFromApi(): Promise<User> {
    const params = this.ctx.params;
    // {
    //   uid: '1',
    // }
    console.log('params:', params);
    return {
      id: 1,
      name: 'testAtQuery',
      age: 1,
    };
  }
}
